const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
    host: '43.143.253.188',
    port: 3306,
    user: 'nextjs',
    password: 'yEAiRE2jcxTyRBy5',
    database: 'nextjs'
};

async function verifyTables() {
    let connection;

    try {
        connection = await mysql.createConnection(dbConfig);
        console.log('🔌 数据库连接成功');

        // 检查坐骑相关表是否存在
        console.log('\n📋 检查坐骑相关表...');

        const [tables] = await connection.execute(`
            SELECT TABLE_NAME, TABLE_COMMENT 
            FROM information_schema.TABLES 
            WHERE TABLE_SCHEMA = 'nextjs' 
            AND TABLE_NAME LIKE '%mount%'
        `);

        if (tables.length === 0) {
            console.log('❌ 没有找到坐骑相关的表');
        } else {
            console.log('✅ 找到以下坐骑相关表:');
            tables.forEach(table => {
                console.log(`   - ${table.TABLE_NAME}: ${table.TABLE_COMMENT || '无注释'}`);
            });
        }

        // 检查表结构
        console.log('\n📋 检查表结构...');

        for (const table of tables) {
            console.log(`\n🔍 表 ${table.TABLE_NAME} 的结构:`);
            const [columns] = await connection.execute(`
                SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_COMMENT
                FROM information_schema.COLUMNS 
                WHERE TABLE_SCHEMA = 'nextjs' 
                AND TABLE_NAME = '${table.TABLE_NAME}'
                ORDER BY ORDINAL_POSITION
            `);

            columns.forEach(col => {
                console.log(`   - ${col.COLUMN_NAME}: ${col.DATA_TYPE} ${col.IS_NULLABLE === 'NO' ? 'NOT NULL' : 'NULL'} ${col.COLUMN_DEFAULT ? `DEFAULT ${col.COLUMN_DEFAULT}` : ''} ${col.COLUMN_COMMENT ? `(${col.COLUMN_COMMENT})` : ''}`);
            });
        }

        console.log('\n🎉 表验证完成！');

    } catch (error) {
        console.error('❌ 验证失败:', error.message);
    } finally {
        if (connection) {
            await connection.end();
            console.log('🔌 数据库连接已关闭');
        }
    }
}

// 执行验证
verifyTables();
